//
//  TDBusinessViewController.m
//  TFB
//
//  Created by Nothing on 15/3/16.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDUserCenterViewController.h"
#import "TDUseHelpTableViewController.h"
#import "TDAboutMineViewController.h"
#import "TDRealNameCertificationViewController.h"
#import "TDVerificationCodeViewController.h"
#import "TDForgetPwdViewController.h"
#import "Toast+UIView.h"
#import "TDUserInfoViewController.h"
#import "TDExampleViewController.h"

#define CALL_ALERT_TAG    100
#define EDITPWD_ALERT_TAG 101
#define CHECK_DEVI_TAG    102

//#define KEFU_PNONENUM @"400-800-1234"
#define VER @"V1.0"
@interface TDUserCenterViewController ()
{
    NSArray *_cellTextArray;
    NSArray *_cellImageArray;
    
    BOOL _isObserving;
}
@end

@implementation TDUserCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:31/255.0f green:121/255.0f blue:132/255.0f alpha:1.0f];
    self.navigationItem.title = @"我的账户";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    _cellTextArray = @[@"修改密码", @"实名认证", @"绑定银行卡", @"我的信息"];
    _cellImageArray = @[@"uc_mima", @"uc_renzheng", @"uc_bangding", @"uc_xinxi"];
    
    self.bottomTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.bottomTableView.tableFooterView = [[UIView alloc] init];

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestForGetCustInfWithCustMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] custId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] complete:^(BOOL succeed, NSString *msg, NSString *cod, TDUser *user) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (succeed) {
            self.userNameLabel.text = [TDUser defaultUser].custName;
            if ([[TDUser defaultUser].custName isEqualToString:@""]) {
                self.userNameLabel.text = [TDUser defaultUser].custId;
            }
            self.userIDLabel.text = [TDUser defaultUser].custLogin;
            
            [self.bottomTableView reloadData];
        }
        else
        {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
    
    [[TDUser defaultUser] addObserver:self forKeyPath:@"custStatus" options:NSKeyValueObservingOptionNew context:nil];
    [[TDUser defaultUser] addObserver:self forKeyPath:@"cardBundingStatus" options:NSKeyValueObservingOptionNew context:nil];
    _isObserving = YES;
}

- (void)dealloc
{
    @try {
        if (_isObserving) {
            [[TDUser defaultUser] removeObserver:self forKeyPath:@"custStatus"];
            [[TDUser defaultUser] removeObserver:self forKeyPath:@"cardBundingStatus"];
        }
    }
    @catch (NSException *exception) {
        
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"custStatus"] || [keyPath isEqualToString:@"cardBundingStatus"]) {
        [self.bottomTableView reloadData];
    }
}

- (IBAction)onClickUserInfoBtn:(id)sender {
    
    TDUserInfoViewController *controller = [[TDUserInfoViewController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark-------
#pragma mark   TableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _cellTextArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndef = @"cellIndef";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndef];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndef];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 26, 26)];
        imageView.tag = 1001;
        [cell.contentView addSubview:imageView];
        
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(imageView.frame.size.width + imageView.frame.origin.x + 8, 2, 80, 40);
        textLabel.font = [UIFont systemFontOfSize:15];
        textLabel.tag = 1002;
        [cell.contentView addSubview:textLabel];
        
        UILabel *rightLabel = [[UILabel alloc] init];
        rightLabel.frame = CGRectMake(CGRectGetMaxX(textLabel.frame) + 8, 2, tableView.bounds.size.width - 10 - CGRectGetMaxX(textLabel.frame) - 8, 40);
        rightLabel.textAlignment = NSTextAlignmentRight;
        rightLabel.font = [UIFont systemFontOfSize:15];
        //154 170 170
        rightLabel.textColor = [UIColor colorWithRed:154/255.0 green:170/255.0 blue:170/255.0 alpha:1.0];
        rightLabel.tag = 1003;
        [cell.contentView addSubview:rightLabel];
    }
    
    UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:1001];
    imageView.image = [UIImage imageNamed:_cellImageArray[indexPath.row]];
    
    UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:1002];
    textLabel.text = _cellTextArray[indexPath.row];
    
    UILabel *rightLabel = (UILabel *)[cell.contentView viewWithTag:1003];
    if (indexPath.row == 1) {
        rightLabel.text = [[TDUser defaultUser] get_custStatus_string];
    } else if (indexPath.row == 2) {
        rightLabel.text = [[TDUser defaultUser] get_cardBundingStatus_string];
    } else {
        rightLabel.text = nil;
    }

    return cell;
}

- (UIView *)creatCellViewWithIndexPath:(NSIndexPath *)indexPath
{
    UIView *bgView = [[UIView alloc] init];
    bgView.tag = 10010;
    bgView.frame = CGRectMake(0, 0, self.bottomTableView.bounds.size.width, 44);
//    bgView.backgroundColor = [UIColor greenColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_cellImageArray[indexPath.row]]];
    imageView.frame = CGRectMake(10, 8, 30, 30);
    [bgView addSubview:imageView];
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.frame = CGRectMake(imageView.frame.size.width + imageView.frame.origin.x + 8, 2, self.bottomTableView.bounds.size.width / 2 - (imageView.frame.size.width + imageView.frame.origin.x + 8) + 40, 40);
    textLabel.text = _cellTextArray[indexPath.row];
    [bgView addSubview:textLabel];
    
    if (indexPath.row == 3) {
                UILabel *verLabel = [[UILabel alloc] init];
                verLabel.frame = CGRectMake(self.bottomTableView.bounds.size.width / 2, 2, self.bottomTableView.bounds.size.width / 2 -10, 40);
                verLabel.text = @"认证状态";
                verLabel.textAlignment = NSTextAlignmentRight;
                //154 170 170
                verLabel.textColor = [UIColor colorWithRed:154/255.0 green:170/255.0 blue:170/255.0 alpha:1.0];
                [bgView addSubview:verLabel];
    }
    
//    if (indexPath.row == 4) {
//        UILabel *phoneNum = [[UILabel alloc] init];
//        phoneNum.frame = CGRectMake(self.bottomTableView.bounds.size.width / 2, 2, self.bottomTableView.bounds.size.width / 2 - 10, 40);
//        phoneNum.text = KEFU_PNONENUM;
//        phoneNum.textAlignment = NSTextAlignmentRight;
//        //154 170 170
//        phoneNum.textColor = [UIColor colorWithRed:154/255.0 green:170/255.0 blue:170/255.0 alpha:1.0];
//        [bgView addSubview:phoneNum];
//    }
//    
//    if (indexPath.row == 6) {
//        UILabel *verLabel = [[UILabel alloc] init];
//        verLabel.frame = CGRectMake(self.bottomTableView.bounds.size.width / 2, 2, self.bottomTableView.bounds.size.width / 2 -10, 40);
//        verLabel.text = VER;
//        verLabel.textAlignment = NSTextAlignmentRight;
//        //154 170 170
//        verLabel.textColor = [UIColor colorWithRed:154/255.0 green:170/255.0 blue:170/255.0 alpha:1.0];
//        [bgView addSubview:verLabel];
//    }
    
    return bgView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    switch (indexPath.row) {
        case 0:
        {
            TDForgetPwdViewController *controller = [[TDForgetPwdViewController alloc] init];
            controller.hidesBottomBarWhenPushed = YES;
            controller.isNeedOldPwd = YES;
            controller.isEditLoginPwd = YES;
            controller.custMobile = [[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE];
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
        case 1:
        {
            if (![[TDUser defaultUser].custStatus isEqualToString:@"1"] &&
                ![[TDUser defaultUser].custStatus isEqualToString:@"2"]) {
                TDExampleViewController *controller = [[TDExampleViewController alloc] init];
                controller.isRealNameCer = YES;
                controller.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:controller animated:YES];
            }
        }
            break;
        case 2:
        {
            if (![[TDUser defaultUser].cardBundingStatus isEqualToString:@"1"] &&
                ![[TDUser defaultUser].cardBundingStatus isEqualToString:@"2"]) {
                TDExampleViewController *controller = [[TDExampleViewController alloc] init];
                controller.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:controller animated:YES];
            }
        }
            break;
        case 3:
        {
            TDUserInfoViewController *controller = [[TDUserInfoViewController alloc] init];
            controller.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
            
        default:
            break;
    }
    
    
}

@end
